package tankproject;

import java.awt.*;
import java.awt.event.*;

public class TankController {

    private Game game;

    private Tank tank;
    private TankView view;

    public TankController(Game game) {
        this.game = game;
        tank = new Tank(game);
        view = new TankView(game, tank);
    }

    public void update() {
        try {
            tank.update();
        } catch (OutOfFuelException e) {
            System.out.println("No more fuel.");
        }
    }

    public void paint(Graphics g) {
        view.paint(g);
    }

    public void keyPressed(KeyEvent ke) {
        int key = ke.getKeyCode();
        if (key == KeyEvent.VK_LEFT || key == KeyEvent.VK_A) {
            tank.turnLeft();
        }

        if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_D) {
            tank.turnRight();
        }

        if (key == KeyEvent.VK_UP || key == KeyEvent.VK_W) {
            tank.speedUp();
        }

        if (key == KeyEvent.VK_DOWN || key == KeyEvent.VK_S) {
            tank.slowDown();
        }
    }

    public void mouseClicked(MouseEvent me) {
        // Left click
        if (me.getButton() == MouseEvent.BUTTON1) {
            try {
                tank.fire();
            } catch (OutOfAmmoException e) {
                System.out.println("Uh oh no more ammo");
            }
        }
    }

}
